<?php
# desc: get top liked comment by URL
# author: thaodx
function getTopFacebookComment($targetUrl, $getAll = false){
	$graphUrl = 'http://graph.facebook.com/comments/?ids=';
	$topComment = array();
	if($targetUrl == '') return false;
	$fbResponse = null;

	if(function_exists('curl_exec')){
		// create a new cURL resource
		$ch = curl_init();
		// set URL and other appropriate options
		curl_setopt($ch, CURLOPT_URL, $graphUrl.$targetUrl);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		// grab URL and pass it to the browser
		$fbResponse = json_decode(curl_exec($ch));
		// close cURL resource, and free up system resources
		curl_close($ch);
	
	}
	else{
		$fbResponse = file_get_contents($graphUrl.$targetUrl);
	}
	if(!empty($fbResponse->$targetUrl->comments->data)){
		$mostLike = -1;
		foreach($fbResponse->$targetUrl->comments->data as $comment){
			if($comment->like_count > $mostLike){
				$mostLike = $comment->like_count;
				$topComment = $comment;
			}
		}
	}
	if($getAll){
		$all = $fbResponse->$targetUrl->comments;
	}
	else{
		$all = null;
	}
	return array('top' => $topComment, 'all' => $all);
}
function getFacebookCount($targetUrl){
	if(is_array($targetUrl)){
		$targetUrl = implode('%27,%27', $targetUrl);
	}
	$graphUrl = 'http://graph.facebook.com/fql?q=SELECT%20like_count,%20comment_count%20FROM%20link_stat%20WHERE%20url%20IN(%27';
	$topComment = array();
	if($targetUrl == '') return false;
	$fbResponse = null;
	$graphUrl = $graphUrl.$targetUrl."%27)";
	
	if(function_exists('curl_exec')){
		// create a new cURL resource
		$ch = curl_init();
		// set URL and other appropriate options
		curl_setopt($ch, CURLOPT_URL, $graphUrl);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		// grab URL and pass it to the browser
		$response = curl_exec($ch);
		$fbResponse = json_decode(curl_exec($ch));
		// close cURL resource, and free up system resources
		curl_close($ch);
	
	}
	else{
		$fbResponse = file_get_contents($graphUrl.$targetUrl);
	}
	

	return $fbResponse->data;
}